<template>
  <div class="container">
    <div class="app-container">
      <div class="role-operate">
        <el-button type="primary" size="mini" @click="showDialog = true">添加角色</el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="name"
          label="角色"
          width="200"
          align="center"
        >
          <template v-slot="{row}">
            <el-input
              v-if="row.isEdit"
              v-model="row.editRow.name"
              size="mini"
            />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="state" align="center" width="200" label="启用">
          <!-- 自定义列结构 -->
          <template v-slot="{ row }">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <el-input
              v-if="row.isEdit"
              v-model="row.editRow.description"
              type="textarea"
            />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini" @click="editConfirm(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit = false">取消</el-button>
            </template>
            <template v-else>
              <!-- 非编辑状态 -->
              <el-button size="mini" type="text">分配权限</el-button>
              <el-button size="mini" type="text" @click="editFn(row)">编辑</el-button>
              <el-popconfirm
                title="确定要删除该角色吗"
                @onConfirm="delFn(row.id)"
              >
                <el-button slot="reference" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <el-pagination
          layout="prev, pager, next"
          :total="pageInfo.total"
          :current-page.sync="pageInfo.page"
          :page-size="pageInfo.pagesize"
          @current-change="pageChangeFn"
        />
      </el-row>
    </div>
    <el-dialog
      title="新增角色"
      :visible.sync="showDialog"
      width="500px"
      @close="closeFn"
    >
      <el-form ref="form" :model="addForm" label-width="120px" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addForm.name" style="width:300px" size="mini" autocomplete="off" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch
            v-model="addForm.state"
            size="mini"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="submit">确定</el-button>
              <el-button size="mini" @click="closeFn">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole, editRole, delRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      addForm: {
        name: '',
        description: '',
        state: 1
      },
      tableData: [],
      pageInfo: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      showDialog: false,
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.pageInfo)
      this.tableData = rows
      this.tableData.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
      this.pageInfo.total = total
    },
    pageChangeFn() {
      this.getRoleList()
    },
    closeFn() {
      this.showDialog = false
      this.$refs.form.resetFields()
    },
    submit() {
      this.$refs.form.validate(async(pass) => {
        if (pass) {
          await addRole(this.addForm)
          this.$message.success('添加角色成功')
          this.getRoleList()
          this.closeFn()
        }
      })
    },
    editFn(row) {
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    async editConfirm(row) {
      if (!row.editRow.name || !row.editRow.description) {
        return this.$message.warning('角色名称或描述不能为空')
      }
      await editRole({ ...row.editRow, id: row.id })
      this.$message.success('角色修改成功')
      Object.assign(row, {
        ...row.editRow,
        isEdit: false // 退出编辑模式
      })
    },
    async delFn(id) {
      await delRole(id)
      this.$message.success('角色删除成功')
      if (this.tableData.length === 1) this.pageInfo.page--
      this.getRoleList()
    }
  }
}
</script>

<style lang="scss" scoped >
.role-operate {
  padding: 10px;
}
</style>
